<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="团体 管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
	        <a-input v-model="queryForm.number" placeholder="请输入团体编号" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
	        <a-input v-model="queryForm.name" placeholder="请输入团体名称" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
	        <a-input v-model="queryForm.createTime" placeholder="请输入录入时间" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
	        <a-input v-model="queryForm.updateTime" placeholder="请输入更新时间" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-button v-permission="['sport:team:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <span>新增</span>
        </a-button>
        <a-tooltip content="导出">
          <a-button v-permission="['sport:team:export']" class="gi_hover_btn-border" @click="onExport">
            <template #icon>
              <icon-download />
            </template>
          </a-button>
        </a-tooltip>
      </template>
      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['sport:team:update']" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['sport:team:delete']"
            status="danger"
            :disabled="record.disabled"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <TeamAddModal ref="TeamAddModalRef" @save-success="search" />
    <TeamDetailDrawer ref="TeamDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import TeamAddModal from './TeamAddModal.vue'
import TeamDetailDrawer from './TeamDetailDrawer.vue'
import { type TeamResp, type TeamQuery, deleteTeam, exportTeam, listTeam } from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import { useDict } from '@/hooks/app'

defineOptions({ name: 'Team' })

const queryForm = reactive<TeamQuery>({
  number: undefined,
  name: undefined,
  createTime: undefined,
  updateTime: undefined,
  sort: ['createTime,desc']
})


const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listTeam({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: '团体编号', dataIndex: 'number', slotName: 'number' },
  { title: '团体名称', dataIndex: 'name', slotName: 'name' },
  { title: '录入时间', dataIndex: 'createTime', slotName: 'createTime' },
  { title: '更新时间', dataIndex: 'updateTime', slotName: 'updateTime' },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['sport:team:update', 'sport:team:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.number = undefined
  queryForm.name = undefined
  queryForm.createTime = undefined
  queryForm.updateTime = undefined
  search()
}

// 删除
const onDelete = (item: TeamResp) => {
  return handleDelete(() => deleteTeam(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportTeam(queryForm))
}

const TeamAddModalRef = ref<InstanceType<typeof TeamAddModal>>()
// 新增
const onAdd = () => {
  TeamAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: TeamResp) => {
  TeamAddModalRef.value?.onUpdate(item.id)
}

const TeamDetailDrawerRef = ref<InstanceType<typeof TeamDetailDrawer>>()
// 详情
const onDetail = (item: TeamResp) => {
  TeamDetailDrawerRef.value?.onDetail(item.id)
}
</script>

<style lang="scss" scoped></style>
